from selenium import webdriver
import time
from selenium.webdriver.common.by import By


class Ecshop:
    def __init__(self):
        self.driver = webdriver.Chrome()
        self.driver.get('https://ecshop.test2.shopex123.com/')
        self.driver.maximize_window()

    def enroll(self, name, em, pw1, pw2):
        # 进入网页后，定位免费注册并点击跳转
        reg = self.driver.find_element(By.CLASS_NAME, 'reg')
        reg.click()
        # 定位用户名 邮箱 密码 确认密码位置并输入
        # 最后定位并点击统一协议并注册按钮

        username = self.driver.find_element(By.ID, 'username')
        username.send_keys(name)

        time.sleep(2)
        email = self.driver.find_element(By.ID, 'email')
        email.send_keys(em)

        time.sleep(2)
        password1 = self.driver.find_element(By.ID, 'password1')
        password1.send_keys(pw1)

        time.sleep(2)
        con_password = self.driver.find_element(By.ID, 'confirm_password')
        con_password.send_keys(pw2)

        button = self.driver.find_element(By.CLASS_NAME, 'signup_button')
        button.click()

    def exit(self):
        # 注册成功后，点击退出
        time.sleep(3)
        exit = self.driver.find_element(By.LINK_TEXT, '退出')
        exit.click()

    def login(self, user_name, paw):
        # 定位用户名 密码，定位登陆按钮并点击
        login = self.driver.find_element(By.CLASS_NAME, 'sign')
        login.click()

        name = self.driver.find_element(By.NAME, 'username')
        name.send_keys(user_name)

        password = self.driver.find_element(By.NAME, 'password')
        password.send_keys(paw)

        time.sleep(2)
        sub = self.driver.find_element(By.NAME, 'submit')
        sub.click()

    def search(self, test):
        #定位搜索框，并输入，最后定位点击按钮
        time.sleep(3)
        key_word = self.driver.find_element(By.ID, 'keyword')
        key_word.send_keys(test)

        btn_search = self.driver.find_element(By.CLASS_NAME, 'btn_search')
        btn_search.click()

    # 退出浏览器
    def quit(self):
        time.sleep(3)
        self.driver.quit()


if __name__ == '__main__':
    ecshop = Ecshop()
    # ecshop.enroll('xixi', '1789@qq.com', '123456', '123456')
    # ecshop.exit()
    ecshop.login('xixi', '123456')
    ecshop.search('香水')
    ecshop.quit()
